﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;

namespace UDPSocketProject
{
    public class UDPSocketClient
    {
        public IPEndPoint ipEndPoint
        {
            get;
            set;
        }

        public UDPSocketClient()
        {
        }

		public UDPSocketClient(IPAddress ipaddress, int port)
		{
			this.ipEndPoint = new IPEndPoint(ipaddress, port);
		}

        public void SendData()
        {
            while (true)
            {
                // UDP 的socket类型不支持stream
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                socket.Connect(this.ipEndPoint);
                Console.WriteLine("请输入你要发送的消息:");
                string SendString = Console.ReadLine();
                byte[] SendBytes = Encoding.Default.GetBytes(SendString);
                socket.Send(SendBytes);
                // 网络接收有延时，因此需要等待服务器进行接收
                socket.Close(100);
            }
        }
    }
}
